<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/myStrategyComment.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/jquery-confirm/css/jquery-confirm.css">
    <script src="/js/plugins/jquery-confirm/js/jquery-confirm.js"></script>
    <script src="/js/common.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script>
        $(function () {
            var imgId;
            var params = getParams();
            if (!params.strategyId) {
                return;
            }
            //获取登录信息
            var user = JSON.parse(sessionStorage.getItem("user"));
            //星星评价
            $(".star i").click(function () {
                //获取num的值
                var num = $(this).data('num');
                //获取class为star下面所有i标签
                var children = $(".star").children('i');
                //防止重选,先将class全部还原为fa-star-o
                $(".star i").removeClass("fa-star").addClass("fa-star-o");
                //遍历所有i标签,将第num的children前的所有children的class属性换为fa-star
                for (var i = 0; i < num; i++) {
                    $(children[i]).removeClass("fa-star-o");
                    $(children[i]).addClass("fa-star");
                }
            });
            //大家都在评标签
            $.get('/tags', function (data) {
                //渲染数据
                $(".tag").renderValues({list: data});
            });

            //上传图片
            $(".rect").click(function () {
                //找到文件框,触发点击事件
                $("#imgInput").click();
                imgId = $(this).data("id");
            });
            //change事件
            $("#imgInput").change(function () {
                //如果有值就提交
                if ($(this).val()) {
                    $("#imgForm").ajaxSubmit(function (data) {
                        if (data.status == 1) {
                            //把新图片的url回显到img
                            $(".rect[data-id='" + imgId + "'] .img").attr('src', data.url);
                        }
                    })
                }
            });
            $("#submitBtn").click(function () {
                //获取评论的相关信息
                //星星数
                var starNum = $(".fa-star").length;
                //获取评价内容
                var content = $("[name=content]").val();
                //获取图片
                var imgUrls = "";
                for (i = 1; i <= 5; i++) {
                    if ($("#img" + i).attr("src")) {
                        imgUrls += $("#img" + i).attr("src") + ";";
                    }
                }
                //新增旅游评论
                $.post('/strategies/'+ params.strategyId +"/"+ user.id+ "/comments",{
                    "starNum":starNum,
                    "content":content,
                    "imgUrls":imgUrls
                }, function (data) {
                    if (data.success) {
                        window.location.href = "/strategyCatalogs.html?strategyId=" + params.strategyId;
                    } else {
                        alert(data.msg);
                    }
                });
                //获取自定义标签内容
                var myTags = $("[name=myTags]").val().split(" ");
                if (myTags.length >= 1 && myTags != false) {
                    for (var i = 0; i < myTags.length; i++) {
                    console.log(myTags[i]);
                        //新增标签
                        $.post('/tags/save/'+ myTags[i]+"/"+params.strategyId, function (data) {
                            if (data.success) {
                                window.location.href = "/strategyCatalogs.html?strategyId=" + params.strategyId;
                            } else {
                                alert(data.msg);
                            }
                        })
                    }
                }
                //获取标签
                var active = $(".active").length;

                for(var i = 0 ; i< active ;i++){
                    var id = $($(".active")[i]).data("id");
                    $.post('/tags/saveRelation/' +id+"/"+params.strategyId, function (data) {
                        if (data.success) {
                            window.location.href = "/strategyCatalogs.html?strategyId=" + params.strategyId;
                        } else {
                            alert(data.msg);
                        }
                    })
                }
            })
        })
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:window.history.go(-1)">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>

        </div>
        <div class="col">
            <span>点评</span>
        </div>
        <div class="col"></div>
    </div>
</div>

<div class="comment">
    <div class="container star">
        <p>你对该旅游地的评价</p>
        <i class="fa fa-star-o fa-lg" data-num="1"></i>
        <i class="fa fa-star-o fa-lg" data-num="2"></i>
        <i class="fa fa-star-o fa-lg" data-num="3"></i>
        <i class="fa fa-star-o fa-lg" data-num="4"></i>
        <i class="fa fa-star-o fa-lg" data-num="5"></i>
    </div>
    <hr/>
    <div class="container tag">
        <p>请为该旅游地选择合适的标签（大家都在评）</p>
        <div class="btn-group-toggle" data-toggle="buttons" render-loop="list">
            <label class="btn btn-outline-danger" render-html="list.name" render-attr="data-id=list.id">
            </label>
        </div>
        <p>添加你自己的标签</p>
        <input name="myTags" class="form-control" placeholder="请使用空格隔开标签，每个标签不超8个字"/>

        <p>更多评价内容</p>
        <textarea name="content" class="form-control" rows="5" placeholder="请填写你对该旅游地的评价"></textarea>
    </div>
    <div class="container imgs ">
        <p>上传图片</p>

        <div class="row">
            <form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
                <input type="file" style="display: none" name="file" id="imgInput"/>
            </form>
            <div class="col-3 rect" data-id="1">
                <img id="img1" class="img" width="100%" height="200px" src=""/>上传
            </div>
            <div class="col-3 rect" data-id="2">
                <img id="img2" class="img" width="100%" height="200px" src=""/>上传
            </div>
            <div class="col-3 rect" data-id="3">
                <img id="img3" class="img" width="100%" height="200px" src=""/>上传
            </div>
            <div class="col-3 rect" data-id="4">
                <img id="img4" class="img" width="100%" height="200px" src=""/>上传
            </div>
            <div class="col-3 rect" data-id="5">
                <img id="img5" class="img" width="100%" height="200px" src=""/>上传
            </div>
        </div>
    </div>

</div>


<div class="operation">
    <button class="btn" onclick="window.history.go(-1)">放弃评价</button>
    <button class="btn" id="submitBtn">提交评价</button>
</div>


</body>

</html>